package com.gzzn.validate.support;

import com.gzzn.validate.internal.InputUtility;
import com.gzzn.validate.internal.Rule;
import com.gzzn.validate.internal.TestResult;

/**
 * author : zdp（zdp118@gmail.com）
 * date   : 2014-3-2
 * 判断两个参数是否相等
 */
public class EqualToValidate extends Validate<Object> {

    final static String messageT = "请输入相同的";

    public EqualToValidate() {
        super(Rule.EqualTo);
    }

    @Override
    public TestResult doValidate(Object input) {
        Object equalsTo = args[0];
		boolean passed;
		if( equalsTo.getClass() != input.getClass() ){
			passed = (InputUtility.isValueType(input) && InputUtility.isValueType(equalsTo)) &&
					Double.valueOf(String.valueOf(input)).equals(Double.valueOf(String.valueOf(equalsTo)));
		}else{
			passed = equalsTo == input || equalsTo.equals(input);
		}
        String message = passed ? null : customMessage != null ? customMessage : messageT;
        return new TestResult(passed,message,rule);
    }
}
